package com.ruoyi.car.service.impl;

import java.util.Iterator;
import java.util.List;

import com.ruoyi.car.domain.CarGarage;
import com.ruoyi.car.domain.CarParkRecord;
import com.ruoyi.car.service.ICarGarageService;
import com.ruoyi.car.service.ICarParkRecordService;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.car.mapper.CarInfoMapper;
import com.ruoyi.car.domain.CarInfo;
import com.ruoyi.car.service.ICarInfoService;

/**
 * 车辆信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-12-02
 */
@Service
public class CarInfoServiceImpl implements ICarInfoService {
    @Autowired
    private CarInfoMapper carInfoMapper;

    @Autowired
    private ICarParkRecordService carParkRecordService;

    @Autowired
    private ICarGarageService carGarageService;

    /**
     * 查询车辆信息
     *
     * @param carId 车辆信息主键
     * @return 车辆信息
     */
    @Override
    public CarInfo selectCarInfoByCarId(Long carId) {
        return carInfoMapper.selectCarInfoByCarId(carId);
    }

    /**
     * 查询车辆信息列表
     *
     * @param carInfo 车辆信息
     * @return 车辆信息
     */
    @Override
    public List<CarInfo> selectCarInfoList(CarInfo carInfo) {
        List<CarInfo> carInfos = carInfoMapper.selectCarInfoList(carInfo);
        for (CarInfo info : carInfos) {
            if(!StringUtils.isEmpty(info.getColour())){
                CarGarage carGarage = carGarageService.selectCarGarageByGarageId(Long.valueOf(info.getColour()));
                info.setPackName(carGarage.getGarageName());
            }
        }
        return carInfos;
    }

    @Override
    public boolean compare(String carNumber) {
        CarInfo carInfo = new CarInfo();
        List<CarInfo> list = carInfoMapper.selectCarInfoList(carInfo);
        Iterator<CarInfo> it = list.iterator();
        while (it.hasNext()) {
            carInfo = it.next();
            if (carNumber.equals(carInfo.getCarNumber())) {
                if ("1".equals(carInfo.getType())) {
                    return true;
                } else {
                    throw new BaseException("车辆状态异常");
                }
            }
        }
        throw new BaseException("车牌号不正确");

    }


    /**
     * 新增车辆信息
     *
     * @param carInfo 车辆信息
     * @return 结果
     */
    @Override
    public int insertCarInfo(CarInfo carInfo) {
        carInfo.setCreateTime(DateUtils.getNowDate());
        return carInfoMapper.insertCarInfo(carInfo);
    }

    /**
     * 修改车辆信息
     *
     * @param carInfo 车辆信息
     * @return 结果
     */
    @Override
    public int updateCarInfo(CarInfo carInfo) {
        carInfo.setUpdateTime(DateUtils.getNowDate());
        return carInfoMapper.updateCarInfo(carInfo);
    }

    /**
     * 根据车牌修改汽车状态
     *
     * @param carInfo
     * @return
     */
    @Override
    public int updateCarInfoByCarNumber(CarInfo carInfo) {
        carInfo.setUpdateTime(DateUtils.getNowDate());
        return carInfoMapper.updateCarInfoByCarNumber(carInfo);
    }


    /**
     * 批量删除车辆信息
     *
     * @param carIds 需要删除的车辆信息主键
     * @return 结果
     */
    @Override
    public int deleteCarInfoByCarIds(Long[] carIds) {
        return carInfoMapper.deleteCarInfoByCarIds(carIds);
    }

    /**
     * 删除车辆信息信息
     *
     * @param carId 车辆信息主键
     * @return 结果
     */
    @Override
    public int deleteCarInfoByCarId(Long carId) {
        return carInfoMapper.deleteCarInfoByCarId(carId);
    }
}
